//
//  WBTabController.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/14.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBTabController.h"
#import "WBNavController.h"
#import "WBTabBar.h"

#import "WBBackGroundView.h"

@interface WBTabController ()

@end

@implementation WBTabController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    WBTabBar *wbTabBar = [[WBTabBar alloc]init];
    [self setValue:wbTabBar forKey:@"tabBar"];
    wbTabBar.btnClickBlock = ^{
    
        NSLog(@"我被点击了，中间按钮");
        
        WBBackGroundView *bgView = [[WBBackGroundView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        
        [bgView moveToVC:self];
    };
    
    
    UIViewController *home = [self navigationController:@"WBHomeController" title:@"首页" imageName:@"tabbar_home" index: 1];
    UIViewController *message = [self navigationController:@"WBMessageController" title:@"消息" imageName:@"tabbar_message_center" index: 2];
    UIViewController *discover = [self navigationController:@"WBDiscoverController" title:@"发现" imageName:@"tabbar_discover" index: 3];
    UIViewController *me = [self navigationController:@"WBMeController" title:@"我的" imageName:@"tabbar_profile" index: 4];
    
    self.viewControllers = @[home,message,discover,me];
}

-(UIViewController *)navigationController:(NSString *)classVc title:(NSString *)title imageName:(NSString *)imageName index:(NSInteger)index{

    Class clz = NSClassFromString(classVc);
    UIViewController *Vc = [[clz alloc]init];
    
    Vc.title = title;
    
    Vc.tabBarItem.tag = index;
    
    [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    
    Vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    Vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    WBNavController *nav = [[WBNavController alloc]initWithRootViewController:Vc];

    return nav;
}
//首页item点击动画效果
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

    NSInteger index = 1;
    for (UIView *subview in tabBar.subviews) {
        
        if ([subview isMemberOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            if (item.tag == index) {
                
                for (UIView *v in subview.subviews) {
                    if ([v isMemberOfClass:NSClassFromString(@"UITabBarSwappableImageView")] ) {
                        
                        v.transform = CGAffineTransformMakeScale(0.6, 0.6);
                        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:8 options:UIViewAnimationOptionCurveEaseIn animations:^{
                            
                            v.transform = CGAffineTransformMakeScale(1, 1);
                            
                        } completion:^(BOOL finished) {
                            
                        }];
                    }
                }
                
                
            }
            index ++ ;
        }
    }
}






@end
